<template>
	<view class="page">
		<view class="wrapper">
			<view class="logo">
				<image class="img" src="../../static/images/logo.png" mode="aspectFit"></image>
			</view>

			<view class="cu-list menu">
				<view class="cu-item">
					<view class="content">
						<text class="text-white text">当前版本</text>
					</view>
					<view class="action">
						<text class="text-grey text-sm">{{version}}</text>
					</view>
				</view>
				<view class="cu-item arrow" @click="update">
					<view class="content">
						<text class="text-white text">获取最新版本</text>
					</view>
				</view>
			</view>

			<u-modal v-model="show" title="下载进度">
				<view class="slot-content padding">
					<u-line-progress :striped="true" :percent="progress" :striped-active="true"></u-line-progress>
				</view>
			</u-modal>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				version: '666',
				name: '666',
				show: false,
				progress: 0
			};
		},
		onLoad() {
			this.check();
		},
		methods: {
			check() {
				plus.runtime.getProperty(plus.runtime.appid, info => {
					this.version = info.version
					this.name = info.name
				})
			},
			update() {
				this.$u.get('/update', {
					version: this.version,
					name: this.name
				}).then(res => {
					let data = res.data.result
					console.log(data)
					if (data.update && data.wgtUrl) {
						let task = uni.downloadFile({
							url: data.wgtUrl,
							success: (downloadResult) => {
								if (downloadResult.statusCode === 200) {
									plus.runtime.install(downloadResult.tempFilePath, {
										force: false
									}, function() {
										console.log('install success...');
										this.show = false
										this.progress = 0
										plus.runtime.restart();
									}, function(e) {
										console.error('install fail...');
									});
								}
							}
						});
						this.show = true
						task.onProgressUpdate((res) => {
							this.progress = res.progress
						});
					} else if (data.update && data.pkgUrl) {
						uni.showModal({
							title: "更新提示",
							content: '有完整包更新，点击查看',
							success: (res) => {
								if (res.confirm) {
									plus.runtime.openURL(data.pkgUrl);
								}
							}
						})
					} else {
						this.$msg(res.data.msg)
					}
				})
			}
		}
	}
</script>

<style>
	.logo {
		padding: 160rpx 0;
		display: flex;
		justify-content: center;
	}

	.logo .img {
		width: 200rpx;
		height: 200rpx;
	}

	.menu .text {
		color: #b3b3ba;
	}

	.cu-item {
		background-color: transparent !important;
	}

	.cu-list.menu>.cu-item .content {
		display: flex;
		align-items: center;
	}

	.cu-list.menu>.cu-item.arrow::before {
		color: #b3b3ba;
	}

	.arrow:active {
		background-color: #659f21 !important;
	}

	.arrow.none::after {
		border-bottom: none !important;
	}
</style>
